﻿using Microsoft.EntityFrameworkCore;
using TxtManager.Core.Entities;

namespace TxtManager.Infrastructure.Context;

public class TextDbContext : DbContext
{
    public DbSet<TextItem> TextItems { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlite("Data Source=texts.db");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // 软删除过滤
        modelBuilder.Entity<TextItem>().HasQueryFilter(e => !e.IsDeleted);

        // 配置主键
        modelBuilder.Entity<TextItem>().HasKey(e => e.Id);

        // 配置字段长度
        modelBuilder.Entity<TextItem>()
            .Property(e => e.Title)
            .HasMaxLength(100);


        // 添加索引
        modelBuilder.Entity<TextItem>()
            .HasIndex(e => e.Title);
        modelBuilder.Entity<TextItem>()
            .HasIndex(e => e.Tag);
    }
}